import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import archivesManage from './modules/archivesManage'
import organizationalStructure from './modules/organizationalStructure'
import myBriefcase from './modules/myBriefcase'
// import digitalKnowledge from './modules/digitalKnowledge'
import systemSettings from './modules/systemSettings'
import preArchiving from './modules/preArchiving'
import nomalAuthManagement from '@/router/modules/nomalAuthManagement'
import nomalArchiveAuditing from '@/router/modules/nomalArchiveAuditing'
import bodyOfKnowledge from './modules/bodyOfKnowledge'
import warehouseManagement from './modules/warehouseManagement'
import knowledgePublish from './modules/knowPublishIssue'
import digitalKnowledgeAccess from './modules/digitalKnowledgeAccess'
import archivesIntellectualization from './modules/archivesIntellectualization'
import mineDigitalArch from './modules/mineDigitalArch'

/**
 * meta : {
    roles: ['admin','editor']    //nomalAuth 普通档案管理员 staff 员工 Manager 主管 Arch 兼职档案专业人员
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/homepage',
    component: () => import('@/views/homepage/index'),
    hidden: true
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/staffLogin',
    component: () => import('@/views/login/staffLogin'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: '首页',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/changePwd',
    component: Layout,
    redirect: '/changePwd',
    hidden: true,
    meta: {
      roles: ['root', 'nomalAuth', 'staff']
    },
    children: [
      {
        path: 'changePwd',
        component: () => import('@/views/dashboard/changePwd.vue'),
        name: '密码修改',
        meta: {
          title: '密码修改',
          roles: ['root', 'nomalAuth', 'staff']
        }
      }
    ]
  },
  archivesManage,
  mineDigitalArch,
  digitalKnowledgeAccess,
  // digitalKnowledge,
  knowledgePublish,
  bodyOfKnowledge,
  nomalAuthManagement,
  nomalArchiveAuditing,
  preArchiving,
  myBriefcase,
  archivesIntellectualization, // 档案知识化先隐藏
  organizationalStructure,
  warehouseManagement,
  systemSettings
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),

  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
